package com.sws.web.controller.ry_rongyue.staff;

import java.util.List;
import jakarta.servlet.http.HttpServletResponse;

import com.sws.rel.service.IPersonDeviceRelService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sws.common.annotation.Log;
import com.sws.common.core.controller.BaseController;
import com.sws.common.core.domain.AjaxResult;
import com.sws.common.enums.BusinessType;
import com.sws.staff.domain.RyStaff;
import com.sws.staff.service.IRyStaffService;
import com.sws.common.utils.poi.ExcelUtil;
import com.sws.common.core.page.TableDataInfo;

/**
 * 工作人员信息Controller
 * 
 * @author cx
 * @date 2025-06-30
 */
@RestController
@RequestMapping("/ry/staff/staff")
public class RyStaffController extends BaseController
{
    @Autowired
    private IRyStaffService ryStaffService;

    @Autowired
    private IPersonDeviceRelService personDeviceRelService;

    /**
     * 查询工作人员信息列表
     */
    @PreAuthorize("@ss.hasPermi('ry/staff:staff:list')")
    @GetMapping("/list")
    public TableDataInfo list(RyStaff ryStaff)
    {
        startPage();
        List<RyStaff> list = ryStaffService.selectRyStaffList(ryStaff);
        return getDataTable(list);
    }

    /**
     * 导出工作人员信息列表
     */
    @PreAuthorize("@ss.hasPermi('ry/staff:staff:export')")
    @Log(title = "工作人员信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RyStaff ryStaff)
    {
        List<RyStaff> list = ryStaffService.selectRyStaffList(ryStaff);
        ExcelUtil<RyStaff> util = new ExcelUtil<RyStaff>(RyStaff.class);
        util.exportExcel(response, list, "工作人员信息数据");
    }

    /**
     * 获取工作人员信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('ry/staff:staff:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        RyStaff staff = ryStaffService.selectRyStaffById(id);
        
        // 查询此人员关联的所有设备
        if (staff != null && staff.getPid() != null && !staff.getPid().isEmpty()) {
            List<String> deviceSns = personDeviceRelService.getDeviceSnsByPersonPid(staff.getPid());
            staff.setDeviceIds(deviceSns);
        }
        
        return success(staff);
    }

    /**
     * 新增工作人员信息
     */
    @PreAuthorize("@ss.hasPermi('ry/staff:staff:add')")
    @Log(title = "工作人员信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RyStaff ryStaff) {
        // 检查设备ID列表
        if (ryStaff.getDeviceIds() == null || ryStaff.getDeviceIds().isEmpty()) {
            return error("请至少选择一个设备");
        }

        try {
            return toAjax(ryStaffService.insertRyStaff(ryStaff));
        } catch (Exception e) {
            logger.error("添加工作人员失败", e);
            return AjaxResult.error("添加工作人员失败：" + e.getMessage());
        }
    }

    /**
     * 修改工作人员信息
     */
    @PreAuthorize("@ss.hasPermi('ry/staff:staff:edit')")
    @Log(title = "工作人员信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RyStaff ryStaff) {
        // 检查设备ID列表
        if (ryStaff.getDeviceIds() == null || ryStaff.getDeviceIds().isEmpty()) {
            return error("请至少选择一个设备");
        }
        
        try {
            return toAjax(ryStaffService.updateRyStaff(ryStaff));
        } catch (Exception e) {
            logger.error("修改工作人员失败", e);
            return AjaxResult.error("修改工作人员失败：" + e.getMessage());
        }
    }

    /**
     * 删除工作人员信息
     */
    @PreAuthorize("@ss.hasPermi('ry/staff:staff:remove')")
    @Log(title = "工作人员信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        try {
            return toAjax(ryStaffService.deleteRyStaffByIds(ids));
        } catch (Exception e) {
            logger.error("删除工作人员失败", e);
            return AjaxResult.error("删除工作人员失败：" + e.getMessage());
        }
    }
}
